//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
var gloData = app.globalData;
var codeString = '';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",
    updateLoginPassword: false,
    // 设置还是修改支付密码?
    setOrUpdate: "设置支付密码",
    // 设置支付密码或输入原支付密码：展示密码输入层、输入的密码、文本框焦点
    payPasswords: "",
    showPayPwdInput: false,
    payPasswords: "",
    payFocus: true,
    // 输入新支付密码：展示密码输入层、输入的密码、文本框焦点
    payPasswordsNew: "",
    showPayPwdInputNew: false,
    payPasswordsNew: "",
    payFocusNew: true,
    // 授权
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    powerFlag: true,
    phoneFlag: true,
    code: 'authMSG',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var uid = wx.getStorageSync('uid');
    var userType = this.data.userType;
    userType = wx.getStorageSync('userType');
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: userType,
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
    })
    if (this.data.isAlreadyPayPassword == "1") {
      this.setData({
        setOrUpdate: "请输入原密码"
      })
    } else {
    }
    // 登录
    var that = this;
    wx.login({
      success(res) {
        //js调用登陆命令获取到code
        if (res.code) {
          codeString = res.code
          //通过code调用自己服务接口获取到openid
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  //获取授权用户的信息
  bindGetUserInfo(e) {
    wx.getSetting({
      success: res => {
        //如果经过授权
        if (res.authSetting['scope.userInfo']) {
          this.setData({
            hideBtn: true
          })
          //获取用户openid
          wx.getUserInfo({
            success: res => {
              var that = this;
              var uid = this.data.uid;
              var resultData = this.data.resultData;
              this.data.userInfo = res.userInfo,
                this.data.userInfo.code = gloData.code,
                // 登录时的code
                // console.log(gloData.code);
                // console.log("method=GetSmallProgramOpenIdFromUserCode&Code=" + codeString + "&UID=" + this.data.uid)
                wx.request({
                  url: baseJs.api + baseJs.encryUrl("method=GetSmallProgramOpenIdFromUserCode&Code=" + codeString + "&UID=" + this.data.uid),
                  success: function (res) {
                    // console.log(res);
                    resultData = JSON.parse(baseJs.decryUrl(res.data));
                    console.log(resultData);
                    // if (app.globalData.userInfo) {
                    that.setData({
                      userInfo: app.globalData.userInfo,
                      // uid: app.globalData.userInfo.uid,
                    })
                    // console.log(gloData.code);
                    // } else {
                    // }
                    // console.log(JSON.parse(baseJs.decryUrl(res.data)))
                  }

                })
              // if (this.userInfoReadyCallback) {
              //   this.userInfoReadyCallback(res)
              // }
            }
          })
        }
      }
    })
  },

  
  // 授权登录
  powerTap: function () {
    this.setData({
      powerFlag: !this.data.powerFlag,
    })
    // 授权成功后返回页面
    // wx.navigateBack({
    //   delta: 1
    // })
    // wx.navigateTo({
    //   url: '../mine/mine',
    // })
    wx.showToast({
      title: '授权成功',
      icon: 'none',
      duration: 2000
    });
  },

  getUserInfo: function (e) {
    var that = this;
    var uid = this.data.uid;
    // console.log(e);
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      hasUserInfo: true,
    })
  },


  // 账户管理
  bindAccount: function () {
    wx.showModal({
      title: '提示',
      content: '功能尚未开通',
      showCancel: false
    })
  },
  // 授权登录
  bindAuthoriza: function () {
    var that = this;
    this.setData({
      powerFlag: false
    })
    // wx.navigateTo({
    //   url: '../authoriza/authoriza',
    // })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 第一个支付密码层
  setPayPassword: function () {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  cancelInputPasswords: function () {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
    });

  },
  // 
  getFocus: function () {
    this.setData({
      payFocus: true
    });
  },
  //
  inputPwd: function (e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //修改密码
  surePasswords: function () {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    //设置还是修改?
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    if (isAlreadyPayPassword == "1") {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
        success: function (res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          console.log(json);
          if (json.ResultFlag == 1) {
            that.setData({
              showPayPwdInputNew: true,
              payFocusNew: true,
              showPayPwdInput: false,
              payFocus: false,
            })
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }
        }
      })
    } else {//设置
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=SetMyTradPassWord&UID=" + uid + "&TradPassword=" + payPasswords),
        success: function (res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          console.log(json);
          if (json.ResultFlag == 1) {
            that.setData({
              showPayPwdInput: false,
              payFocus: false,
              payPasswords: "",
              isAlreadyPayPassword:1
            })
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
            wx.setStorageSync('isAlreadyPasswords', 1)
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }
        }
      })
    }

  },
  // 第二个密码层：隐藏支付密码输入层、获取焦点、输入密码,监听、输入密码 确定 
  cancelInputPasswordsNew: function () {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInputNew: false,
      payFocusNew: false,
      payPasswordsNew: ''
    });

  },
  // 
  getFocusNew: function () {
    this.setData({
      payFocusNew: true
    });
  },
  //
  inputPwdNew: function (e) {
    this.setData({
      payPasswordsNew: e.detail.value
    });
  },
  //新支付密码-确定
  surePasswordsNew: function () {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var payPasswordsNew = this.data.payPasswordsNew;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=UpUserTradePwdByUID&UID=" + uid + "&YuanTradePWd=" + payPasswords + "&NewTradePWd=" + payPasswordsNew),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          that.setData({
            showPayPwdInputNew: false,
            payFocusNew: false,
            payPasswordsNew: '',
            payPasswords: ''
          })
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },

  // 

})